<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>WeakMap 类</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="weakreference.get.html">« WeakReference::get</a></li>
      <li style="float: right;"><a href="ext-weakmap.construct.html">WeakMap::__construct »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="reserved.interfaces.html">预定义接口和类</a></li>
    <li>WeakMap 类</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.weakmap" class="reference">

 <h1 class="title">WeakMap 类</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 8)</p>

  
  <div class="section" id="weakmap.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    <span class="classname"><strong class="classname">WeakMap</strong></span> 是将对象作为 key 来访问的 map（或者说字典）。然而，与其它类似 <span class="classname"><a href="class.splobjectstorage.html" class="classname">SplObjectStorage</a></span> 
    不同，<span class="classname"><strong class="classname">WeakMap</strong></span> 中的对象 key 不影响对象的引用计数。也就是说，如果在任何时候对其唯一的剩余引用是 
    <span class="classname"><strong class="classname">WeakMap</strong></span> key，那么该对象将会被垃圾收集并从 <span class="classname"><strong class="classname">WeakMap</strong></span> 
    移除。它的主要用法是从对象中编译数据派生缓存，这种场景下不需要存活得比对象更久。
   </p>
   <p class="para">
    <span class="classname"><strong class="classname">WeakMap</strong></span> 实现了 <span class="interfacename"><a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span>、
    <span class="interfacename"><a href="class.iterator.html" class="interfacename">Iterator</a></span>、<span class="interfacename"><a href="class.countable.html" class="interfacename">Countable</a></span>，
    因此大多数情况下，它能和关联数组一样使用。
   </p>
  </div>
  

  <div class="section" id="weakmap.synopsis">
   <h2 class="title">类摘要</h2>

   
   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">final</span>
      <span class="modifier">class</span> <strong class="classname">WeakMap</strong>
     </span>

     <span class="oointerface"><span class="modifier">implements</span> 
       <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span><span class="oointerface">,  <a href="class.countable.html" class="interfacename">Countable</a></span><span class="oointerface">,  <a href="class.iteratoraggregate.html" class="interfacename">IteratorAggregate</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="ext-weakmap.construct.html" class="methodname">__construct</a></span>()</div>

    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.count.html" class="methodname">count</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.getiterator.html" class="methodname">getIterator</a></span>(): <span class="type"><a href="class.iterator.html" class="type Iterator">Iterator</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.offsetexists.html" class="methodname">offsetExists</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.offsetget.html" class="methodname">offsetGet</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.offsetset.html" class="methodname">offsetSet</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="weakmap.offsetunset.html" class="methodname">offsetUnset</a></span>(<span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>): <span class="type"><span class="type void">void</span></span></div>

   }</div>
   

  </div>
  
  <div class="section" id="weakmap.examples">
   <h2 class="title">范例</h2>
   <p class="para">
    <div class="example" id="example-404">
     <p><strong>示例 #1 <span class="classname"><strong class="classname">Weakmap</strong></span> 用法示例</strong></p>
     <div class="example-contents">
      <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$wm&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">WeakMap</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$o&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">StdClass</span><span style="color: #007700">;<br /><br />class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__destruct</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Dead!\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$wm</span><span style="color: #007700">[</span><span style="color: #0000BB">$o</span><span style="color: #007700">]&nbsp;=&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$wm</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"Unsetting...\n"</span><span style="color: #007700">;<br />unset(</span><span style="color: #0000BB">$o</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Done\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$wm</span><span style="color: #007700">));</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
      <div class="cdata"><pre>
int(1)
Unsetting...
Dead!
Done
int(0)
</pre></div>
     </div>
    </div>
   </p>
  </div>
  

 </div>

 





 







 







 








 








 








 









<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="ext-weakmap.construct.html">WeakMap::__construct</a> — 实例化一个新的 map</li><li><a href="weakmap.count.html">WeakMap::count</a> — 统计 map 中存活实体的数量</li><li><a href="weakmap.getiterator.html">WeakMap::getIterator</a> — 接收一个外部迭代器</li><li><a href="weakmap.offsetexists.html">WeakMap::offsetExists</a> — 检测 map 中是否存在某个对象</li><li><a href="weakmap.offsetget.html">WeakMap::offsetGet</a> — 返回某个对象指向的值</li><li><a href="weakmap.offsetset.html">WeakMap::offsetSet</a> — 更新 map 新的键值对</li><li><a href="weakmap.offsetunset.html">WeakMap::offsetUnset</a> — 从 map 中移除一条</li></ul>
</div>
</div></div></body></html>